package com.example.piece.ui.adapter

import android.graphics.Color
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.TextPaint
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.text.style.ForegroundColorSpan
import android.view.View
import android.widget.Toast
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.example.base.ext.navigationTo
import com.example.base.ext.setCornersPlaceHolderImageUrl
import com.example.piece.BR
import com.example.piece.R
import com.example.piece.constant.Key
import com.example.piece.constant.RoutePath
import com.example.piece.databinding.ItemHomeSearchHistoryBinding
import com.example.piece.databinding.ItemMsgBinding
import com.example.piece.databinding.ItemPingLunBinding
import com.example.piece.databinding.ItemSystemMsgBinding
import com.example.piece.entity.UserAttentionListEntity
import com.example.piece.entity.UserCommentListEntity
import com.example.piece.entity.UserMessageSystemEntity

class PingLunAdapter :
    BaseQuickAdapter<UserCommentListEntity, BaseDataBindingHolder<ItemPingLunBinding>>(
        R.layout.item_ping_lun
    ), LoadMoreModule {


    override fun convert(
        holder: BaseDataBindingHolder<ItemPingLunBinding>,
        item: UserCommentListEntity,
    ) {
        val binding =
            holder.dataBinding
        val variable = binding?.setVariable(BR.entity, item)
        binding?.setVariable(BR.adapter, this)
        binding?.setVariable(BR.position, holder.layoutPosition)

        val format = String.format(item.msgItemTypeDesc, item.targetNickname)
            val spannableStringBuilder = SpannableStringBuilder ()
        //设置文字
        spannableStringBuilder.append(format)
        val lastIndexOf = format.lastIndexOf(item.targetNickname)



        spannableStringBuilder.setSpan(
            ForegroundColorSpan(
                Color.parseColor(
                    "#46AFFF"
                )
            ), lastIndexOf, lastIndexOf+item.targetNickname.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
        )

        spannableStringBuilder.setSpan(object : ClickableSpan() {
            override fun onClick(widget: View) {
                navigationTo(RoutePath.MAIN_INFO_ACTIVITY, mutableMapOf(
                    Key.DataKey.INFO_TYPE to 0,
                    Key.DataKey.TARGET_USER_ID to item.targetUserId))
            }

            override fun updateDrawState(ds: TextPaint) {
                super.updateDrawState(ds)
                ds.isUnderlineText = false;

            }
        }, lastIndexOf, lastIndexOf+item.targetNickname.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
        binding?.tv?.movementMethod = LinkMovementMethod.getInstance()

        binding?.tv?.text =spannableStringBuilder
            binding?.executePendingBindings()
    }


}